package 
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.utils.*;
	
	/**
	 * ...
	 * @author Lorenzo Nuvoletta
	 */
	public class Main extends Sprite 
	{
		private var myGrid:GenerateGraphicGrid;
		private var myAstar:AStar;
		private var columns:int = 100;
		private var rows:int = 100;
		private var time:int;		
		
		public function Main():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			// entry point			
			myGrid = new GenerateGraphicGrid(columns, rows);			
			addChild(myGrid);
			myGrid.x = myGrid.y = 30;
			myGrid.scaleX = myGrid.scaleY = .2;
			
			var text:TextField = new TextField();
			
			myAstar = new AStar(myGrid.nodesList, columns);
			var path:Vector.<int>;
			
			time = getTimer();			
			path = myAstar.checkPath(0, myGrid.nodesList.length-1, true, true);	
			text.text = 'Time: ' + String(getTimer() - time);
			
			addChild(text);
			
			for (var i:int = 0; i < path.length; i++ ) {
				var ball:Ball = new Ball();
				ball.x = myGrid.nodesList[path[i]].posX;
				ball.y = myGrid.nodesList[path[i]].posY;
				myGrid.addChild(ball);
				if (path[i] == myAstar.startNode || path[i] == myAstar.endNode) {
					ball.scaleX = ball.scaleY = 2;
				}
			}
		}
		
	}
	
}